﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Reflection;

namespace QBS.Tools
{
    [AttributeUsage(AttributeTargets.Field)]
    public class EnumExtension : Attribute
    {
        private string title;
        public EnumExtension(string title)
        {
            this.title = title;
        }
        public static string Get(Type tp, string name)
        {
            MemberInfo[] mi = tp.GetMember(name);
            if (mi != null && mi.Length > 0)
            {
                EnumExtension attr = Attribute.GetCustomAttribute(mi[0], typeof(EnumExtension)) as EnumExtension;
                if (attr != null)
                {
                    return attr.title;
                }
            }
            return null;
        }
        public static string Get(object enm)
        {
            if (enm != null)
            {
                MemberInfo[] mi = enm.GetType().GetMember(enm.ToString());
                if (mi != null && mi.Length > 0)
                {
                    EnumExtension attr = Attribute.GetCustomAttribute(mi[0], typeof(EnumExtension)) as EnumExtension;
                    if (attr != null)
                    {
                        return attr.title;
                    }
                }
            }
            return null;
        }
    }
    public enum JsonStatus
    {
        [EnumExtension("自行导出")]
        USE = -1,
        [EnumExtension("操作完成 !")]
        SUCC = 0,
        [EnumExtension("支付宝 !")]
        ALIPAY = 1,
        [EnumExtension("微信支付 !")]
        WXPAY = 2,

        [EnumExtension("操作失败 !")]
        FAIL = 1001,
        [EnumExtension("权限不足 !")]
        PRIV = 1002,
        [EnumExtension("不存在的数据")]
        NODATA = 1003,
        [EnumExtension("数据校验失败")]
        VERIFY = 1004,
        [EnumExtension("参数传递错误,如有疑问请联系管理员!")]
        PARAMERR = 1005,
        [EnumExtension("帐号重复登录")]
        LOGGED = 1010,
        [EnumExtension("帐号已退出登陆,请先登录 !")]
        LOGOUT = 1011,
        [EnumExtension("用户被冻结")]
        USERLOCK = 1012,
        [EnumExtension("没有绑定OPENID")]
        NOOPENID = 1013,

        [EnumExtension("IP绑定!")]
        BINDINGIP = 2001,
        [EnumExtension("硬件绑定!")]
        BINDINGHARDWARE = 2002,
        [EnumExtension("账号到期!")]
        EXPIRE = 2003,

        [EnumExtension("未知异常,如有疑问请联系管理员!")]
        EXCEPTION = 1000
    }
    public class JsonData
    {
        private JsonStatus _status;
        private string _msg;
        /// <summary>
        /// 标识是否成功
        /// </summary>
        public JsonStatus status { get { return _status; } set { _status = value; icon = _status == JsonStatus.SUCC ? 1 : 2; } }
        /// <summary>
        /// 返回的JSON数据
        /// </summary>
        public object data { get; set; }
        /// <summary>
        /// 返回需要跳转的URL
        /// </summary>
        //  public string url { get; set; }
        /// <summary>
        /// 返回的消息
        /// </summary>
        public string msg
        {
            get
            {
                if (!string.IsNullOrEmpty(_msg)) { return _msg; }
                return EnumExtension.Get(status);
            }
            set { _msg = value; }
        }
        /// <summary>
        /// 返回数据的条数
        /// </summary>
        public int total { get; set; }
        public int icon { get; set; }
    }
    public class JsonUtil
    {
        private bool is_end;
        private JsonData json = new JsonData();
        public JsonUtil(bool end = true)
        {
            is_end = end;
            json.status = JsonStatus.PRIV;
            json.data = null;
            json.msg = "权限不足";
        }

        public JsonUtil(JsonStatus status, string data, string msg)
        {
            json.status = status;
            json.data = data;
            json.msg = msg;
        }
        public object JsonData { get; set; }
        public JsonStatus Status { get { return json.status; } set { json.status = value; } }
        public object Data { get { return json.data; } set { json.data = value; } }
        //  public string Url { get { return json.url; } set { json.url = value; } }
        public string Msg { get { return json.msg; } set { json.msg = value; json.status = JsonStatus.FAIL; } }
        public int Total { get { return json.total; } set { json.total = value; } }

        public void Set(bool status, string msg = "", object data = null, int total = 0)
        {
            json.status = status ? JsonStatus.SUCC : JsonStatus.FAIL;
            json.data = data;
            //    json.url = url;
            json.msg = msg;
            json.total = total;
        }
        public void Set(JsonStatus status, string msg = "", object data = null, int total = 0)
        {
            json.status = status;
            json.data = data;
            //  json.url = url;
            json.msg = msg;
            json.total = total;
        }
        public override string ToString()
        {
            //JavaScriptSerializer jss = new JavaScriptSerializer();
            return json.enJson();
        }
        public void Show()
        {
            if (json.data == null)
                json.data = "";
            if (JsonData != null)
                HttpContext.Current.Response.Write(JsonData.enJson());
            else
                HttpContext.Current.Response.Write(json.enJson());

            if (is_end)
                HttpContext.Current.Response.End();
        }
        public void Show(JsonStatus status, string msg = "")
        {
            json.status = status;
            json.msg = msg;
            // json.url = url;
            if (json.data == null)
                json.data = "";
            if (JsonData != null)
                HttpContext.Current.Response.Write(JsonData.enJson());
            else
                HttpContext.Current.Response.Write(json.enJson());
            if (is_end)
                HttpContext.Current.Response.End();
        }
        public void Show(bool status, string msg = "", object data = null)
        {
            json.status = status ? JsonStatus.SUCC : JsonStatus.FAIL; ;
            json.msg = msg;
            //json.url = url;
            if (data != null)
                json.data = data;
            if (JsonData != null)
                HttpContext.Current.Response.Write(JsonData.enJson());
            else
                HttpContext.Current.Response.Write(json.enJson());
            if (is_end)
                HttpContext.Current.Response.End();
        }
    }
}
